#include "dkglTestIntersection.h"
#include "dkglOBB.h"
#include <list>

namespace DKGL
{
	namespace physics
	{
		using namespace bounding;
		std::list<OrientedBoundingBox*> OBBList;

		void AddOrientedBoundingBox(OrientedBoundingBox* obb)
		{
			OBBList.push_back(obb);
		}


		class OBBCollsionTester
		{
		public:

			OBBCollsionTester(OrientedBoundingBox* a) : _a(a)
			{
			}

			void operator()(OrientedBoundingBox* _b)
			{
				if (_a == _b)
					return;

				bool r = OrientedBoundingBox::testIntersection(*_a, *_b);

				if (r)
				{
					r = r;
				}
			}

		private:

			OrientedBoundingBox* _a;
		};



		void EvalOrientedBoundingBoxCollsion()
		{
			if (OBBList.size()<2)
				return;

			std::list<OrientedBoundingBox*>::iterator cur(OBBList.begin());
			std::list<OrientedBoundingBox*>::iterator next = cur;
			next++;
			std::list<OrientedBoundingBox*>::iterator end(OBBList.end());

			while (next!=end)
			{
				OrientedBoundingBox* _a = *cur;
				std::for_each(cur, OBBList.end(), OBBCollsionTester(_a));
				++cur;
				++next;
			}
		}
		
		/*INTERSECTION_TEST_RESULT
		PlaneAABBIntersect(const AABB& aabb, const Plane& plane)
		{
			Vector3 vmin, vmax;
			for (int i=0;i<3;i++)
			{
				if (plane.n[i]>=0)
				{
					vmin[i] = aabb.min[i];
					vmax[i] = aabb.max[i];
				}
				else
				{
					vmin[i] = aabb.max[i];
					vmax[i] = aabb.min[i];
				}
			}

			if (plane.n.dotProduct(vmin) + plane.d > 0)
				return OUTSIDE;
			if (plane.n.dotProduct(vmax) + plane.d < 0)
				return INSIDE;
			return INTERSECTIN;
		}


		INTERSECTION_TEST_RESULT
		PlaneOBBIntersect(const OBB& obb, const Plane& plane)
		{
			Vector3 normal = Vector3(
				obb.Axis[0].dotProduct(plane.n),
				obb.Axis[1].dotProduct(plane.n),
				obb.Axis[2].dotProduct(plane.n));

			Vector3 vmin, vmax;
			for (int i=0;i<3;i++)
			{
				if (normal[i]>=0)
				{
					vmin[i] = obb.min[i];
					vmax[i] = obb.max[i];
				}
				else
				{
					vmin[i] = obb.max[i];
					vmax[i] = obb.min[i];
				}
			}

			if (normal.dotProduct(vmin) + plane.d > 0)
				return OUTSIDE;
			if (normal.dotProduct(vmax) + plane.d < 0)
				return INSIDE;
			return INTERSECTIN;
		}*/


	}
}